import {
    searchGiftBindPage,
    searchGiftPage, searchGoodsPage,

    updateGift
} from '../../api/gift'
import {
    deleteBindGoodList,
    addBindGoodList,
    deleteBindGiftList,
    addBindGiftList,
    searchGoodListPage,
    searchGiftGoodCheck,
    deleteBindById,
    searchGiftGoodColors,
    searchGiftGoodBrands,
    searchGiftGoodTypes,
    saveGiftGood,
    getGiftBindList,
    getGoodBindList,
    updateGiftGood
} from '../../api/gift'
import pickerOptions from '@/utils/date';
import {getBase64Str} from '../../utils/base64Utils'
import base64 from '../../utils/getbase64Str2';


let giftBind = {
    data() {
        return {
            addData:{},
            giftinnerVisible: false,
            goodinnerVisible: false,
            //日期组件
            pickerOptions: pickerOptions,
            //table的list
            tableData: [],
            giftTableData: [],
            goodTableData: [],
            addGiftBind:[],
            addGoodBind:[],
            total: 0,
            bindParams: {},
            totalGift: 0, //入库量+赠品量的总和
            searchParams: {
                currentPage: 1,
                pageSize: 10
            },
            searchGiftParams: {
                currentPage: 1,
                pageSize: 12
            },
            searchGListParams: {
                currentPage: 1,
                pageSize: 12
            },
            colorList: [],
            brandList: [],
            typeList: [],
            giftCheckList: [],
            innerVisible: false,
            dialogVisibleChooseGift: false,
            formDialogVisible: false,
            editDialogVisible: false,
            checkDialogVisible: false,
            formData: {},
            editForm: {},
            giftListForm: [],
            goodListForm: [],
            imageUrl: ''
        };
    },
    methods: {
        // indexMethod(index) {
        //     let curpage = this.currentPage; //单前页码，具体看组件取值
        //     let limitpage = this.PageSize; //每页条数，具体是组件取值
        //     return index + 1 + (curpage - 1) * limitpage;
        // },

        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange(val) {
            this.response = val;
            console.log("checkbox:", this.response);
        },
        currentPageChange(currentPage) {
            //console.log(currentPage);
            this.searchParams.currentPage = currentPage;
            this.findPage();
        },
        currentGiftPageChange(currentPage) {
            //console.log(currentPage);
            this.searchGiftParams.currentPage = currentPage;
            this.findGiftPage();
        },
        currentGoodPageChange(currentPage) {
            //console.log(currentPage);
            this.searchGListParams.currentPage = currentPage;
            this.findGiftPage();
        },
        /*  findPage(){
              searchPage(this.searchParams).then(response=>{
                 this.total= response.total;
                 this.tableData = response.data;
              })
          },  */
        //第二种写法：
        async findPage() {
            let response = await searchGiftBindPage(this.searchParams);
            this.total = response.total;
            console.log(response.data);
            this.tableData = response.data;
        },
        async findGiftPage() {
            let response = await searchGiftPage(this.searchGiftParams);
            this.total = response.total;
            console.log(response.data);
            this.giftTableData = response.data;

        },
        async findGoodPage() {
            let response = await searchGoodListPage(this.searchGListParams);
            this.total = response.total;
            console.log(response.data);
            this.giftTableData = response.data;
        },
        chooseTime() {
            this.searchParams.startTime = this.pickerOptions.startArrayDate[0];
            this.searchParams.endTime = this.pickerOptions.startArrayDate[1];
        },
        resetForm() {
            this.searchParams = {currentPage: 1, pageSize: 10};
            this.pickerOptions.startArrayDate = '';
            this.findPage();

        },
        //async表示异步调用 await：表示执行完这条语句，才执行下面语句
        async deleteById() {
            console.log(this.formData.giftBindid);
            await deleteBindById(this.formData.bindId);
            this.findPage();
        },

        async init() {
            let response1 = await searchGiftGoodColors();
            let response2 = await searchGiftGoodBrands();
            let response3 = await searchGiftGoodTypes();
            let response4 = await searchGiftGoodCheck();
            this.colorList = response1;
            this.brandList = response2;
            this.typeList = response3;
            this.giftCheckList = response4;
        },
        getImgStr(e) {
            base64.getbase64Str(e.file).then(response => {
                this.formData.brandLogo = response;
                this.imageUrl = response;
            })

        },
        async addGiftGood() {
            this.formData.giftSum = this.formData.giftSum - this.formData.giftNum;
            await updateGift(this.formData);
            console.log(this.formData)
            await saveGiftGood(this.formData);
            this.findPage();
        },
        async updateGiftList(){
            this.addGiftBind=JSON.parse(JSON.stringify(this.giftListForm));
            await this.giftListForm.forEach((value,index) => {
                if (value.bindId!==null){
               deleteBindGiftList(value.bindId);
            }
            });
            await this.addGiftBind.forEach((value,index) => {
                let addBindId=value.bindId;
                let addGiftId=value.giftId;
                this.$set(this.addData, "bindId", addBindId);
                this.$set(this.addData, "giftId", addGiftId);
                console.log("this.addData"+this.addData);
                addBindGiftList(this.addData);
            })
        },
        async updateGoodList(){
            this.addGoodBind=JSON.parse(JSON.stringify(this.goodListForm));
            await this.goodListForm.forEach((value,index) => {
                if (value.bindId!==null){
                    deleteBindGoodList(value.bindId);
                }
            });
            await this.addGoodBind.forEach((value,index) => {
                let addBindId=value.bindId;
                let addGoodId=value.goodId;
                this.$set(this.addData, "bindId", addBindId);
                this.$set(this.addData, "goodId", addGoodId);
                console.log("this.addData"+this.addData);
                addBindGoodList(this.addData);
            })
        },
        async getThisGiftList(row) {
            this.bindParams.bindId = await row.bindId;
            console.log("this.bindParams.bindId:", this.bindId);
            let response = await getGiftBindList(this.bindParams);
            console.log("response:", response);
            this.giftListForm = response;
        },
        async getThisGoodList(row) {
            this.bindParams.bindId = await row.bindId;
            console.log("this.bindParams.bindId:", this.bindId);
            let response = await getGoodBindList(this.bindParams);
            console.log("response:", response);
            this.goodListForm = response;

        },
        async editGiftGood() {
            this.editForm.giftSum = this.totalGift - this.editForm.giftNum;
            await updateGift(this.editForm);
            await updateGiftGood(this.editForm);
            this.findPage();
        },
        async chooseThisGift(row) {
            console.log("-----------");
            console.log(this.giftListForm);
            this.giftListForm.push(row);
        },
        async chooseThisGood(row) {
            this.goodListForm.push(row);
        },
        async giftData() {
            let vm =this;
            let response = await searchGiftPage(this.searchGiftParams);
            this.total = response.total;
            console.log(response.total);
            this.giftTableData = response.data;
            let temp = 0;
            this.giftListForm.forEach(val =>{
                this.giftTableData.forEach((value,index) => {
                    if (val.giftId === value.giftId){
                        console.log(value);
                        temp = index;
                    }
                })
                this.giftTableData.splice(temp,1);
            })
        },
        async goodData() {
            let response = await searchGoodListPage(this.searchGListParams);
            this.total = response.total;
            console.log(response.total);
            this.goodTableData = response.data;
            let temp = 0;
            this.goodListForm.forEach(val =>{
                this.goodTableData.forEach((value,index) => {
                    if (val.goodId === value.goodId){
                        console.log(value);
                        temp = index;
                    }
                })
                this.goodTableData.splice(temp,1);
            })
        },
        delMulGiftData() {
            //拿到选中的数据
            let val = this.multipleSelection
            //如果选中数据存在
            if (val) {
                //将选中数据遍历
                val.forEach((val, index) => {
                    //遍历源数据
                    this.giftListForm.forEach((v, i) => {
                        //如果选中数据和源数据的某一条唯一标识符相等，删除对应的源数据
                        if (val.giftId === v.giftId) {
                            this.giftListForm.splice(i, 1)
                        }
                    })
                })
            }
            //清除选中状态
            this.$refs.multipleTable.clearSelection()
        },
        delMulGoodData() {
            //拿到选中的数据
            let val = this.multipleSelection
            //如果选中数据存在
            if (val) {
                //将选中数据遍历
                val.forEach((val, index) => {
                    //遍历源数据
                    this.goodListForm.forEach((v, i) => {
                        //如果选中数据和源数据的某一条唯一标识符相等，删除对应的源数据
                        if (val.goodId === v.goodId) {
                            this.goodListForm.splice(i, 1)
                        }
                    })
                })
            }
            //清除选中状态
            this.$refs.multipleTable.clearSelection()
        },
        //得到选中数据
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        parseDateFull(time) {
            var x = new Date(time);
            var z = {
                y: x.getFullYear(),
                M: x.getMonth() + 1,
                d: x.getDate(),
                h: x.getHours(),
                m: x.getMinutes(),
                s: x.getSeconds(),
            };
            if (z.M < 10) {
                z.M = "0" + z.M;
            }
            if (z.d < 10) {
                z.d = "0" + z.d;
            }
            if (z.h < 10) {
                z.h = "0" + z.h;
            }
            if (z.m < 10) {
                z.m = "0" + z.m;
            }
            return z.y + "年" + z.M + "月" + z.d + "日" + z.h + "时" + z.h + "分";
        }
    },
    created() {
        this.findPage();
        this.init();
        this.giftData();
    }
};

export default giftBind;
